//
//  GDFMDBViewController.m
//  Gunterapple
//
//  Created by 热带雨林 on 2021/2/13.
//  Copyright © 2021 热带雨林. All rights reserved.
//

#import "GDFMDBViewController.h"
#import "fmdbModel.h"

static NSString *cellID = @"cellID";

@interface GDFMDBViewController ()<UITableViewDelegate,UITableViewDataSource,UIViewGDButtonDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, assign) CGRect rect;
@property (nonatomic, strong)NSMutableArray *dataArray;
@end

@implementation GDFMDBViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self reloadAllData];
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectZero];
    headerView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableHeaderView = headerView;
    headerView.delegate = self;
    NSArray *btnArr = @[@"创建数据库并添加数据",@"插入单条数据",@"修改单条数据",@"插入多条数据",@"删除一条数据",@"删除所有数据",@"查询单条数据",@"查询所有数据"];
    GDWeakSelf;
    [headerView creatNormalButtonWithTitleArray:btnArr withRect:^(CGRect rect) {
        weakSelf.rect = rect;
    }];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(SJHeight);
        make.left.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(self.rect.size.width, self.rect.size.height));
    }];
}
- (void)addData{
    NSString *title = [[NSArray arrayWithObjects:@"路飞",@"索隆",@"乌索普",@"娜美",@"山治",@"乔巴",@"妮可罗宾",@"弗兰克",@"布鲁克",@"甚平", nil] objectAtIndex:arc4random() % 9];
    NSString *name = [[NSArray arrayWithObjects:@"坦克",@"射手",@"法师",@"刺客", nil] objectAtIndex:arc4random() % 3];
    NSString *desc = [[NSArray arrayWithObjects:@"武器",@"属性",@"技能",@"速度", nil] objectAtIndex:arc4random() % 3];
    NSString *infor = [[NSArray arrayWithObjects:@"主力",@"辅助",@"战士",@"打野", nil] objectAtIndex:arc4random() % 3];
    
    fmdbModel *model = [[fmdbModel alloc]init];
    model.title = title;
    model.name = name;
    model.desc = desc;
    model.infor = infor;
    //插入数据
    [[GDDBDatabaseManager shareDBManager] insertDataWithModel:model withFileName:testTableName];
    
    [self reloadAllData];
    
}
- (void)reloadAllData{
    [self.dataArray removeAllObjects];
    NSArray *dataArr = [[GDDBDatabaseManager shareDBManager]getAllDataWithModelClass:[fmdbModel class] withFileName:testTableName];
    [self.dataArray addObjectsFromArray:dataArr];
}

- (void)clickButtonWithTag:(NSInteger)tag{
    switch (tag) {
        case 10:{
            
        }
            break;
        case 11:{
            [self addData];
        }
            break;
        case 12:{
            
        }
            break;
        case 13:{
            
        }
            break;
        case 14:{
            
        }
            break;
        case 15:{
            
        }
            break;
        case 16:{
            
        }
            break;
        case 17:{
            [self reloadAllData];
        }
            break;
        default:
            break;
    }
    [self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    fmdbModel *model = self.dataArray[indexPath.row];
    cell.textLabel.text = model.name;
    return cell;
}
- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, GDScreenW, GDScreenH) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (NSMutableArray *)dataArray{
    if(!_dataArray){
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
